﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Project;
using AMM.DataAccess.Store;
using AMM.Model;
using AMM.Model.Project;

namespace AMM.Business.Project {
    public class ProductProcessor : BaseProcessor {

        private int _pageSize;
        private ProductRepository _productRepository;

        public ProductProcessor(int pageSize = int.MaxValue) {
            this._pageSize = pageSize;
            this._productRepository = new ProductRepository(pageSize);
        }

        public Product FetchProductById(int productId) {
            try {
                return this._productRepository.FetchProductById(productId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定成品资料数据失败！");
            }
        }

        public List<Product> FetchProductList() {
            try {
                return this._productRepository.FetchProductList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取成品资料列表失败！");
            }
        }

        public List<ProductViewModel> FetchProductViewModelListByVendorId(int vendorId) {
            try {
                List<OutsourceQuotation> resultList = new OutsourceQuotationRepository(int.MaxValue).FetchOutsourceQuotationsByVendorId(vendorId);
                return resultList.Select(x => {
                    return new ProductViewModel() {
                        Key = x.Product.ProductId,
                        ProductType = x.Product.Basic.Name,
                        Code = x.Product.Code,
                        Name = x.Product.Name,
                        Remarks = x.Product.Remarks
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取成品资料列表失败！");
            }
        }

        public List<ProductViewModel> FetchProductViewModelList(ProductSearchModel searchModel, int pageIndex = 1) {
            try {
                List<Product> resultList = this._productRepository.FetchProductList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new ProductViewModel() {
                        Key = x.ProductId,
                        ProductType = x.Basic.Name,
                        Unit = x.Basic1.Name,
                        Code = x.Code,
                        Name = x.Name,
                        Remarks = x.Remarks,
                        IsEnabled = x.IsEnabled,
                        IsSelfFinished = x.IsSelfFinished,
                        IsAttach = x.AttachmentId.HasValue,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取成品资料列表失败！");
            }
        }

        public void Insert(Product product) {
            try {
                product.ProductId = Guid.NewGuid().GetHashCode();
                product.CreatedDatetime = DateTime.Now;
                this._productRepository.Insert(product);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新成品资料失败！");
            }
        }

        public void Update(Product product) {
            try {
                Product oldProduct = this.FetchProductById(product.ProductId);
                if (!oldProduct.RowVersion.SequenceEqual(product.RowVersion)) {
                    throw this.BuildException("更新成品资料失败，数据不一致性错误！");
                }
                product.ModifiedDatetime = DateTime.Now;
                this._productRepository.Update(product, oldProduct);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新成品资料失败！");
            }
        }

        public void Delete(Product product) {
            try {
                this._productRepository.Delete(product);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除成品资料失败！");
            }
        }

        public void Delete(params int[] productIds) {
            try {
                this._productRepository.DeleteMultipleProduct(productIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除成品资料失败！");
            }
        }
    }
}
